// pagesA/loginI/loginI.js
import {
  validate
} from '../../common/js/validate'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobileDisable: true,
    msg: '发送验证码',
    count: 60,
    timer: null,
    send: false,
    username:'',
    tabNav:0,
    disabled: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      tabNav:options.tabNav
    })
    console.log(options.tabNav);
  },
  showDialog({
    title,
    tip,
    icon
  } = {}) {
    this.setData({
      title: title,
      tip: tip,
      icon: icon
    })
    this.selectComponent('#dialog').show()
  },
  registerLink(){
    wx.redirectTo({
      url: '../loginI/loginI?tabNav='+this.data.tabNav,
    })
  },
  bindinputmobile: function (e) {
    if(e.detail.value.length==11){
      let urls = '/customer/check/'
    wx.request({
      url: app.globalData.serverUrl + urls + e.detail.value,
      method: 'GET',
      success:(res=>{
        if(res.data.code==200){
          app.showMsg(res.data.message)
          this.setData({
            mobileDisable: e.detail.value,
            phoneNumber: e.detail.value,
            mobileErr: '',
            codeErr: ''
          })
        }else{
          this.setData({
            mobileDisable: !e.detail.value,
            phoneNumber: e.detail.value,
            mobileErr: '',
            codeErr: ''
          })
        }
      })
    })
    }
  },
  bindinputcode: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  bindinputname:function(e){
    let that = this
    if(e.detail.value){
      that.setData({
        username:e.detail.value
      })
    }
  },
  toggle(e) {
    this.setData({
      disabled: !e.detail.checked
    })
  },
  bindtapLogin(){
    let that = this
    let data ={
      code:that.data.code,
      username:that.data.username,
      phoneNumber:that.data.phoneNumber
    }
    if(data.username=='' || data.username==undefined || data.phoneNumber=='' || data.phoneNumber==undefined || data.code=='' || data.code==undefined){
      this.showDialog({
        title: '请正确输入',
        icon: 'warning',
        tip: ['手机号、姓名及验证码']
      })
      return
    }
    wx.request({
      url: app.globalData.serverUrl+'/customer/register',
      method:'POST',
      data,
      success:(res=>{
        let resp = res.data
        if(resp.code==200){
          wx.showToast({
            title: '注册成功自动返回登录',
            mask:true,
            icon:'none',
            success:(res=>{
              that.registerLink()
            })
          })
        }else{
          app.showMsg(resp.message)
        }
      })
    })
  },
  tapSendCode: function () {
    const self = this
    if(!validate(this, 'phoneNumber')){
      return
    }
    wx.showLoading({
      mask:true
    })
    if (this.data.send) {
      return
    }
    self.setData({
      send: true,
      mobileDisable:true
    })
    wx.request({
      url: app.globalData.serverUrl+'/user/code/'+self.data.phoneNumber,
      method: 'GET',
      success(res) {
        self.timer = setInterval(() => {
          wx.hideLoading()
          let count = self.data.count - 1
          let msg = count + '秒后重新发送'
          if (count === 0) {
            clearInterval(self.timer)
            msg = "重新发送"
            self.setData({
              send: false,
              count: 60,
              msg: msg,
              mobileDisable:false
            })
            return
          }
          self.setData({
            count: count,
            msg: msg
          })
        }, 1000)
      },
      error(data) {
        self.setData({
          send: false
        })
        app.showMsg(data.msg)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})